<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta http-equiv="refresh" content="10">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>共享文档</title>
	<link rel="icon" type="image/x-icon" href="luckysheet/img/favicon.ico"/>
	<link rel="stylesheet" type="text/css" href="luckysheet/css/normalize.css" />
	<link rel="stylesheet" type="text/css" href="luckysheet/css/htmleaf-demo.css">
	<link rel="stylesheet" href="luckysheet/css/style.css">
	<style>
	   #backButton {
		position: absolute;
    	right: 0px;
		top: 4px; /* 添加top属性 */
		background-color: red;
        color: white;
		}
		#logoutButton {
		position: absolute;
    	right: 100px;
		top: 4px; /* 添加top属性 */
		background-color: red;
        color: white;
		}
		#loginButton {
		 position: absolute;
		 right: 200px;
		 top: 4px; /* 添加top属性 */
		 background-color: rgb(235, 240, 243);
		 color: rgb(8, 8, 8);
		 }
    </style>
</head>
<body>
<div class="htmleaf-container">
	<header class="htmleaf-header">
		<button id="loginButton" class="btn btn-primary btn-warning" style=" padding:5px 10px; font-size: 12px; margin-right: 10px;"></button>
		<button id="logoutButton" class="btn btn-primary btn-warning" style=" padding:5px 10px; font-size: 12px; margin-right: 10px;" onclick="logout()">退出登录</button>
		<button id="backButton" class="btn btn-primary btn-info" style=" padding:5px 10px; font-size: 12px;" onclick="goBack()">返回</button>
		<h1>QA内部共享文档</h1>
	</header>
	<main id=app>
		
		<mochi-box shiba="random">
			<h3>
				新建表格
			</h3>
			<a href="/index/create"> <img src="luckysheet/img/create.png" /></a>
		</mochi-box>

		<mochi-box shiba="random">
			<h3>
				导入表格
			</h3>
			<a href="/import"> <img src="luckysheet/img/create.png" /></a>
		</mochi-box>


		<#list all as wb>

		<mochi-box shiba="random" >
			<h4>
				<a target="_self" href="/index/${wb.id}">${wb.option.title}</a>
			</h4>
			<h5>
				<a href="/index" onclick="deleteSheet('${wb.id}')">删除</a>
				<a target="_blank" href="index/export/${wb.id}">导出</a>
			</h5>
			<a target="_self" href="/index/${wb.id}"> <img src="luckysheet/img/excel.png" /></a>
		</mochi-box>
	</#list>

	</main>

</div>
<script src='luckysheet/js/vue.min.js'></script>
<script  src="luckysheet/js/script.js"></script>
<script src="luckysheet/js/luckyexcel.umd.js"></script>
<script src="luckysheet/libs/jquery-1.12.4/jquery.min.js"></script>
<!--<script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>-->
<script>

	function deleteSheet(webId) {
		$.ajax({
			type: 'GET',
			url: "delete/" + webId,
			dataType: 'json',
			cache: false, 
			headers: {
				'Content-Type': 'application/json;charset=utf-8'
			},
			success: function(data, status) {
				console.error("成功:success");
        },
		error: function(jqXHR, textStatus, errorThrown) {
            console.error("请求失败:", textStatus, errorThrown);
        }
		});
	}
	function exportSheet(webId) {
	$.ajax({
			type: 'GET',
			url: "index/export/" + webId,
			dataType: 'json',
			cache: false, // 异步请求时false
			headers: {
				'Content-Type': 'application/json;charset=utf-8'
			},
			success: function(webId, status) {
				console.log("success");
			}
		});
	}

	// 退出登录函数
    function logout() {
        $.ajax({
            type: 'POST',
            url: '/user/logout',
            dataType: 'json',
            cache: false,
            headers: {
                'Content-Type': 'application/json;charset=utf-8'
            },
            success: function(response) {
                if (response.code === '0000') { // 假设成功码为 '0000'
                    window.location.href = '/login'; // 跳转到登录页面
                } else {
					console.error('退出登录失败:', response.message);
					window.location.href = '/login'; // 跳转到登录页面
                }
            },
            error: function(error) {
                console.error('请求出错:', error);
				window.location.href = '/login'; // 跳转到登录页面
            }
        });
    }

	// 返回函数
	function goBack() {
		window.history.back(); 
        }
	function getCookie(name) {
		// 获取当前页面的所有 Cookie 信息
		const cookies = document.cookie.split('; ');
		// 遍历所有的 Cookie
		for (let i = 0; i < cookies.length; i++) {
			const cookie = cookies[i].split('=');
			// 如果找到指定名称的 Cookie
			if (cookie[0] === name) {
				// 返回该 Cookie 的值
				return cookie[1];
			}
		}
		// 如果未找到指定名称的 Cookie，返回 null
		return null;
	}

	// 调用 getCookie 函数获取 sheetUserInfo 的值
	const userName = getCookie('sheetUserInfo');
	if (userName) {
		const loginButtonElement = document.getElementById('loginButton');
		loginButtonElement.textContent = userName;
	} else {
		window.location.href = '/login'; 
	}
</script>

</body>
</html>